page.tsx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. "use client";
  2. import {fetchApi, fetchFile} from "@/app/_modules/func";
  3. import {DeleteOutlined, ExclamationCircleFilled, PlusOutlined, ReloadOutlined,} from "@ant-design/icons";
  4. import type {ActionType, ProColumns, ProFormInstance,} from "@ant-design/pro-components";
  5. import {
  6. ModalForm,
  7. PageContainer,
  8. ProForm,
  9. ProFormDigit,
  10. ProFormRadio,
  11. ProFormSelect,
  12. ProFormText,
  13. ProFormTextArea,
  14. ProTable,
  15. } from "@ant-design/pro-components";
  16. import {Button, message, Modal, Space, Tag} from "antd";
  17. import {useRouter} from "next/navigation";
  18. import {faCheck, faDownload, faPenToSquare, faToggleOff, faToggleOn, faXmark,} from "@fortawesome/free-solid-svg-icons";
  19. import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
  20. import {useRef, useState} from "react";
  21. //查询类型详情
  22. const queryTypeAPI = "/api/system/dict/type";
  23. //查询所有类型列表
  24. const queryTypeListAPI = "/api/system/dict/type/optionselect";
  25. //查询表格数据API
  26. const queryAPI = "/api/system/dict/data/list";
  27. //新建数据API
  28. const newAPI = "/api/system/dict/data";
  29. //修改数据API
  30. const modifyAPI = "/api/system/dict/data";
  31. //查询详情数据API
  32. const queryDetailAPI = "/api/system/dict/data";
  33. //删除API
  34. const deleteAPI = "/api/system/dict/data";
  35. //导出API
  36. const exportAPI = "/api/system/dict/data/export";
  37. //导出文件前缀名
  38. const exportFilePrefix = "data";
  39. export default function DictData({ params }: { params: { dictid: string } }) {
  40. const { push } = useRouter();
  41. const [defaultType, setDefaultType] = useState("");
  42. //获取对应的字典类型的值
  43. const getTypeData = async () => {
  44. const resp = await fetchApi(`${queryTypeAPI}/${params.dictid}`, push);
  45. if (resp != undefined) {
  46. if (searchTableFormRef.current) {
  47. searchTableFormRef.current.setFieldsValue({
  48. dictType: resp.data.dictType,
  49. });
  50. }
  51. setDefaultType(resp.data.dictType);
  52. return resp.data.dictType;
  53. }
  54. return "";
  55. };
  56. //查询字典类型列表
  57. const getTypeList = async () => {
  58. const dataArray: Array<any> = new Array<any>();
  59. const resp = await fetchApi(queryTypeListAPI, push);
  60. if (resp != undefined) {
  61. resp.data.forEach((item: any) => {
  62. const type = {
  63. label: item.dictName,
  64. value: item.dictType,
  65. };
  66. dataArray.push(type);
  67. });
  68. }
  69. return dataArray;
  70. };
  71. //表格列定义
  72. const columns: ProColumns[] = [
  73. {
  74. title: "字典名称",
  75. dataIndex: "dictType",
  76. valueType: "select",
  77. fieldProps: {
  78. allowClear: false,
  79. },
  80. request: getTypeList,
  81. hideInTable: true,
  82. order: 3,
  83. },
  84. {
  85. title: "数据编码",
  86. dataIndex: "dictCode",
  87. search: false,
  88. },
  89. {
  90. title: "数据标签",
  91. fieldProps: {
  92. placeholder: "请输入数据标签",
  93. },
  94. dataIndex: "dictLabel",
  95. order: 2,
  96. render: (_, record) => {
  97. const isTag = record.listClass === "";
  98. let tagColor = "default";
  99. if (record.listClass === "") {
  100. return _;
  101. } else {
  102. switch (record.listClass) {
  103. case "default":
  104. tagColor = "processing";
  105. break;
  106. case "primary":
  107. tagColor = "processing";
  108. break;
  109. case "success":
  110. tagColor = "success";
  111. break;
  112. case "info":
  113. tagColor = "default";
  114. break;
  115. case "warning":
  116. tagColor = "warning";
  117. break;
  118. case "danger":
  119. tagColor = "error";
  120. break;
  121. default:
  122. tagColor = "processing";
  123. break;
  124. }
  125. return (
  126. <Space>
  127. <Tag color={tagColor}>{_}</Tag>
  128. </Space>
  129. );
  130. }
  131. },
  132. },
  133. {
  134. title: "数据键值",
  135. dataIndex: "dictValue",
  136. search: false,
  137. },
  138. {
  139. title: "数据排序",
  140. dataIndex: "dictSort",
  141. sorter: true,
  142. search: false,
  143. },
  144. {
  145. title: "状态",
  146. fieldProps: {
  147. placeholder: "请选择数据状态",
  148. },
  149. dataIndex: "status",
  150. valueType: "select",
  151. render: (_, record) => {
  152. return (
  153. <Space>
  154. <Tag
  155. color={record.status === "0" ? "green" : "red"}
  156. icon={
  157. record.status == 0 ? (
  158. <FontAwesomeIcon icon={faCheck} />
  159. ) : (
  160. <FontAwesomeIcon icon={faXmark} />
  161. )
  162. }
  163. >
  164. {_}
  165. </Tag>
  166. </Space>
  167. );
  168. },
  169. valueEnum: {
  170. 0: {
  171. text: "正常",
  172. status: "0",
  173. },
  174. 1: {
  175. text: "停用",
  176. status: "1",
  177. },
  178. },
  179. order: 1,
  180. },
  181. {
  182. title: "备注",
  183. dataIndex: "remark",
  184. search: false,
  185. },
  186. {
  187. title: "创建时间",
  188. dataIndex: "createTime",
  189. valueType: "dateTime",
  190. search: false,
  191. },
  192. {
  193. title: "操作",
  194. key: "option",
  195. search: false,
  196. render: (_, record) => [
  197. <Button
  198. key="modifyBtn"
  199. type="link"
  200. icon={<FontAwesomeIcon icon={faPenToSquare} />}
  201. onClick={() => onClickShowRowModifyModal(record)}
  202. >
  203. 修改
  204. </Button>,
  205. <Button
  206. key="deleteBtn"
  207. type="link"
  208. danger
  209. icon={<DeleteOutlined />}
  210. onClick={() => onClickDeleteRow(record)}
  211. >
  212. 删除
  213. </Button>,
  214. ],
  215. },
  216. ];
  217. //0.查询表格数据
  218. const queryTableData = async (params: any, sorter: any, filter: any) => {
  219. const searchParams = {
  220. pageNum: params.current,
  221. ...params,
  222. };
  223. delete searchParams.current;
  224. const queryParams = new URLSearchParams(searchParams);
  225. //如果没有带上默认的字典类型,查询绑定上
  226. if (!("dictType" in searchParams)) {
  227. const defaultType = await getTypeData();
  228. queryParams.append("dictType", defaultType);
  229. }
  230. Object.keys(sorter).forEach((key) => {
  231. queryParams.append("orderByColumn", key);
  232. if (sorter[key] === "ascend") {
  233. queryParams.append("isAsc", "ascending");
  234. } else {
  235. queryParams.append("isAsc", "descending");
  236. }
  237. });
  238. const body = await fetchApi(`${queryAPI}?${queryParams}`, push);
  239. return body;
  240. };
  241. //1.新建
  242. //确定新建数据
  243. const executeAddData = async (values: any) => {
  244. const body = await fetchApi(newAPI, push, {
  245. method: "POST",
  246. headers: {
  247. "Content-Type": "application/json",
  248. },
  249. body: JSON.stringify(values),
  250. });
  251. if (body != undefined) {
  252. if (body.code == 200) {
  253. message.success(body.msg);
  254. if (actionTableRef.current) {
  255. actionTableRef.current.reload();
  256. }
  257. return true;
  258. }
  259. message.error(body.msg);
  260. return false;
  261. }
  262. return false;
  263. };
  264. //2.修改
  265. //是否展示修改对话框
  266. const [isShowModifyDataModal, setIsShowModifyDataModal] = useState(false);
  267. //展示修改对话框
  268. const onClickShowRowModifyModal = (record?: any) => {
  269. queryRowData(record);
  270. setIsShowModifyDataModal(true);
  271. };
  272. //修改数据表单引用
  273. const modifyFormRef = useRef<ProFormInstance>(null);
  274. //操作当前数据的附加数据
  275. const [operatRowData, setOperateRowData] = useState<{
  276. [key: string]: any;
  277. }>({});
  278. //查询并加载待修改数据的详细信息
  279. const queryRowData = async (record?: any) => {
  280. const dictCode =
  281. record !== undefined ? record.dictCode : selectedRow.dictCode;
  282. operatRowData["dictCode"] = dictCode;
  283. setOperateRowData(operatRowData);
  284. if (dictCode !== undefined) {
  285. const body = await fetchApi(`${queryDetailAPI}/${dictCode}`, push);
  286. if (body !== undefined) {
  287. if (body.code == 200) {
  288. modifyFormRef?.current?.setFieldsValue({
  289. //需要加载到修改表单中的数据
  290. dictType: body.data.dictType,
  291. dictLabel: body.data.dictLabel,
  292. dictValue: body.data.dictValue,
  293. dictSort: body.data.dictSort,
  294. status: body.data.status,
  295. listClass: body.data.listClass,
  296. cssClass: body.data.cssClass,
  297. remark: body.data.remark,
  298. });
  299. }
  300. }
  301. }
  302. };
  303. //确认修改数据
  304. const executeModifyData = async (values: any) => {
  305. values["dictCode"] = operatRowData["dictCode"];
  306. const body = await fetchApi(modifyAPI, push, {
  307. method: "PUT",
  308. headers: {
  309. "Content-Type": "application/json",
  310. },
  311. body: JSON.stringify(values),
  312. });
  313. if (body !== undefined) {
  314. if (body.code == 200) {
  315. message.success(body.msg);
  316. //刷新列表
  317. if (actionTableRef.current) {
  318. actionTableRef.current.reload();
  319. }
  320. setIsShowModifyDataModal(false);
  321. return true;
  322. }
  323. message.error(body.msg);
  324. return false;
  325. }
  326. };
  327. //3.删除
  328. //点击删除按钮,展示删除确认框
  329. const onClickDeleteRow = (record?: any) => {
  330. const dictCode =
  331. record != undefined ? record.dictCode : selectedRowKeys.join(",");
  332. Modal.confirm({
  333. title: "系统提示",
  334. icon: <ExclamationCircleFilled />,
  335. content: `确定删除字典编码为“${dictCode}”的数据项?`,
  336. onOk() {
  337. executeDeleteRow(dictCode);
  338. },
  339. onCancel() {},
  340. });
  341. };
  342. //确定删除选中的数据
  343. const executeDeleteRow = async (dictCode: any) => {
  344. const body = await fetchApi(`${deleteAPI}/${dictCode}`, push, {
  345. method: "DELETE",
  346. });
  347. if (body !== undefined) {
  348. if (body.code == 200) {
  349. message.success("删除成功");
  350. //修改按钮变回不可点击
  351. setRowCanModify(false);
  352. //删除按钮变回不可点击
  353. setRowCanDelete(false);
  354. //选中行数据重置为空
  355. setSelectedRowKeys([]);
  356. //刷新列表
  357. if (actionTableRef.current) {
  358. actionTableRef.current.reload();
  359. }
  360. } else {
  361. message.error(body.msg);
  362. }
  363. }
  364. };
  365. //4.导出
  366. //导出表格数据
  367. const exportTable = async () => {
  368. if (searchTableFormRef.current) {
  369. const formData = new FormData();
  370. const data = {
  371. pageNum: page,
  372. pageSize: pageSize,
  373. ...searchTableFormRef.current.getFieldsValue(),
  374. };
  375. Object.keys(data).forEach((key) => {
  376. if (data[key] !== undefined) {
  377. formData.append(key, data[key]);
  378. }
  379. });
  380. await fetchFile(
  381. exportAPI,
  382. push,
  383. {
  384. method: "POST",
  385. body: formData,
  386. },
  387. `${exportFilePrefix}_${new Date().getTime()}.xlsx`
  388. );
  389. }
  390. };
  391. //5.选择行
  392. //选中行操作
  393. const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);
  394. const [selectedRow, setSelectedRow] = useState(undefined as any);
  395. //修改按钮是否可用,选中行时才可用
  396. const [rowCanModify, setRowCanModify] = useState(false);
  397. //删除按钮是否可用,选中行时才可用
  398. const [rowCanDelete, setRowCanDelete] = useState(false);
  399. //ProTable rowSelection
  400. const rowSelection = {
  401. onChange: (newSelectedRowKeys: React.Key[], selectedRows: any[]) => {
  402. setSelectedRowKeys(newSelectedRowKeys);
  403. setRowCanDelete(newSelectedRowKeys && newSelectedRowKeys.length > 0);
  404. if (newSelectedRowKeys && newSelectedRowKeys.length == 1) {
  405. setSelectedRow(selectedRows[0]);
  406. setRowCanModify(true);
  407. } else {
  408. setRowCanModify(false);
  409. setSelectedRow(undefined);
  410. }
  411. },
  412. //复选框的额外禁用判断
  413. // getCheckboxProps: (record) => ({
  414. // disabled: record.userId == 1,
  415. // }),
  416. };
  417. //搜索栏显示状态
  418. const [showSearch, setShowSearch] = useState(true);
  419. //action对象引用
  420. const actionTableRef = useRef<ActionType>(null);
  421. //搜索表单对象引用
  422. const searchTableFormRef = useRef<ProFormInstance>(null!);
  423. //当前页数和每页条数
  424. const [page, setPage] = useState(1);
  425. const defaultPageSize = 10;
  426. const [pageSize, setPageSize] = useState(defaultPageSize);
  427. const pageChange = (page: number, pageSize: number) => {
  428. setPage(page);
  429. setPageSize(pageSize);
  430. };
  431. return (
  432. <PageContainer
  433. header={{
  434. title: "字典数据",
  435. onBack(e) {
  436. push("/system/dict");
  437. },
  438. }}
  439. >
  440. <ProTable
  441. formRef={searchTableFormRef}
  442. rowKey="dictCode"
  443. rowSelection={{
  444. selectedRowKeys,
  445. ...rowSelection,
  446. }}
  447. columns={columns}
  448. request={async (params: any, sorter: any, filter: any) => {
  449. // 表单搜索项会从 params 传入,传递给后端接口。
  450. const data = await queryTableData(params, sorter, filter);
  451. if (data !== undefined) {
  452. return Promise.resolve({
  453. data: data.rows,
  454. success: true,
  455. total: data.total,
  456. });
  457. }
  458. return Promise.resolve({
  459. data: [],
  460. success: true,
  461. });
  462. }}
  463. pagination={{
  464. defaultPageSize: defaultPageSize,
  465. showQuickJumper: true,
  466. showSizeChanger: true,
  467. onChange: pageChange,
  468. }}
  469. search={
  470. showSearch
  471. ? {
  472. defaultCollapsed: false,
  473. searchText: "搜索",
  474. }
  475. : false
  476. }
  477. dateFormatter="string"
  478. actionRef={actionTableRef}
  479. toolbar={{
  480. actions: [
  481. <ModalForm
  482. key="addmodal"
  483. title="添加字典数据"
  484. trigger={
  485. <Button icon={<PlusOutlined />} type="primary">
  486. 新建
  487. </Button>
  488. }
  489. autoFocusFirstInput
  490. modalProps={{
  491. destroyOnHidden: true,
  492. }}
  493. submitTimeout={2000}
  494. onFinish={executeAddData}
  495. >
  496. <ProForm.Group>
  497. <ProFormText
  498. width="md"
  499. name="dictType"
  500. label="字典类型"
  501. initialValue={defaultType}
  502. disabled
  503. />
  504. </ProForm.Group>
  505. <ProForm.Group>
  506. <ProFormText
  507. width="md"
  508. name="dictLabel"
  509. label="数据标签"
  510. rules={[{ required: true, message: "请输入数据标签" }]}
  511. />
  512. <ProFormText
  513. width="md"
  514. name="dictValue"
  515. label="数据键值"
  516. rules={[{ required: true, message: "请输入数据键值" }]}
  517. />
  518. </ProForm.Group>
  519. <ProForm.Group>
  520. <ProFormDigit
  521. fieldProps={{ precision: 0 }}
  522. width="md"
  523. name="dictSort"
  524. initialValue="0"
  525. label="数据排序"
  526. placeholder="请输入数据排序"
  527. rules={[{ required: true, message: "请输入数据排序" }]}
  528. />
  529. <ProFormRadio.Group
  530. width="md"
  531. name="status"
  532. label="状态"
  533. initialValue="0"
  534. options={[
  535. {
  536. label: "正常",
  537. value: "0",
  538. },
  539. {
  540. label: "停用",
  541. value: "1",
  542. },
  543. ]}
  544. />
  545. </ProForm.Group>
  546. <ProForm.Group>
  547. <ProFormSelect
  548. width="md"
  549. name="listClass"
  550. label="回显样式"
  551. valueEnum={{
  552. default: {
  553. text: "默认(default)",
  554. status: "default",
  555. },
  556. primary: {
  557. text: "主要(primary)",
  558. status: "primary",
  559. },
  560. success: {
  561. text: "成功(成功)",
  562. status: "success",
  563. },
  564. info: {
  565. text: "信息(info)",
  566. status: "info",
  567. },
  568. warning: {
  569. text: "警告(warning)",
  570. status: "warning",
  571. },
  572. danger: {
  573. text: "危险(danger)",
  574. status: "danger",
  575. },
  576. }}
  577. />
  578. <ProFormText width="md" name="cssClass" label="样式属性" />
  579. </ProForm.Group>
  580. <ProFormTextArea
  581. name="remark"
  582. width={688}
  583. label="备注"
  584. placeholder="请输入内容"
  585. />
  586. </ModalForm>,
  587. <ModalForm
  588. key="modifymodal"
  589. title="修改岗位"
  590. formRef={modifyFormRef}
  591. trigger={
  592. <Button
  593. icon={<FontAwesomeIcon icon={faPenToSquare} />}
  594. disabled={!rowCanModify}
  595. onClick={() => onClickShowRowModifyModal()}
  596. >
  597. 修改
  598. </Button>
  599. }
  600. open={isShowModifyDataModal}
  601. autoFocusFirstInput
  602. modalProps={{
  603. destroyOnHidden: true,
  604. onCancel: () => {
  605. setIsShowModifyDataModal(false);
  606. },
  607. }}
  608. submitTimeout={2000}
  609. onFinish={executeModifyData}
  610. >
  611. <ProForm.Group>
  612. <ProFormText
  613. width="md"
  614. name="dictType"
  615. label="字典类型"
  616. disabled
  617. />
  618. </ProForm.Group>
  619. <ProForm.Group>
  620. <ProFormText
  621. width="md"
  622. name="dictLabel"
  623. label="字典标签"
  624. rules={[{ required: true, message: "请输入字典标签" }]}
  625. />
  626. <ProFormText
  627. width="md"
  628. name="dictValue"
  629. label="字典键值"
  630. rules={[{ required: true, message: "请输入字典键值" }]}
  631. />
  632. </ProForm.Group>
  633. <ProForm.Group>
  634. <ProFormDigit
  635. fieldProps={{ precision: 0 }}
  636. width="md"
  637. name="dictSort"
  638. initialValue="0"
  639. label="显示排序"
  640. placeholder="请输入显示排序"
  641. rules={[{ required: true, message: "请输入显示排序" }]}
  642. />
  643. <ProFormRadio.Group
  644. width="md"
  645. name="status"
  646. label="状态"
  647. initialValue="0"
  648. options={[
  649. {
  650. label: "正常",
  651. value: "0",
  652. },
  653. {
  654. label: "停用",
  655. value: "1",
  656. },
  657. ]}
  658. />
  659. </ProForm.Group>
  660. <ProForm.Group>
  661. <ProFormSelect
  662. width="md"
  663. name="listClass"
  664. label="回显样式"
  665. valueEnum={{
  666. default: {
  667. text: "默认(default)",
  668. status: "default",
  669. },
  670. primary: {
  671. text: "主要(primary)",
  672. status: "primary",
  673. },
  674. success: {
  675. text: "成功(成功)",
  676. status: "success",
  677. },
  678. info: {
  679. text: "信息(info)",
  680. status: "info",
  681. },
  682. warning: {
  683. text: "警告(warning)",
  684. status: "warning",
  685. },
  686. danger: {
  687. text: "危险(danger)",
  688. status: "danger",
  689. },
  690. }}
  691. />
  692. <ProFormText width="md" name="cssClass" label="样式属性" />
  693. </ProForm.Group>
  694. <ProFormTextArea
  695. name="remark"
  696. width={688}
  697. label="备注"
  698. placeholder="请输入内容"
  699. />
  700. </ModalForm>,
  701. <Button
  702. key="danger"
  703. danger
  704. icon={<DeleteOutlined />}
  705. disabled={!rowCanDelete}
  706. onClick={() => onClickDeleteRow()}
  707. >
  708. 删除
  709. </Button>,
  710. <Button
  711. key="export"
  712. type="primary"
  713. icon={<FontAwesomeIcon icon={faDownload} />}
  714. onClick={exportTable}
  715. >
  716. 导出
  717. </Button>,
  718. ],
  719. settings: [
  720. {
  721. key: "switch",
  722. icon: showSearch ? (
  723. <FontAwesomeIcon icon={faToggleOn} />
  724. ) : (
  725. <FontAwesomeIcon icon={faToggleOff} />
  726. ),
  727. tooltip: showSearch ? "隐藏搜索栏" : "显示搜索栏",
  728. onClick: (key: string | undefined) => {
  729. setShowSearch(!showSearch);
  730. },
  731. },
  732. {
  733. key: "refresh",
  734. tooltip: "刷新",
  735. icon: <ReloadOutlined />,
  736. onClick: (key: string | undefined) => {
  737. if (actionTableRef.current) {
  738. actionTableRef.current.reload();
  739. }
  740. },
  741. },
  742. ],
  743. }}
  744. />
  745. </PageContainer>
  746. );
  747. }